﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Deploy.Middleware
{
    public class PreOptionRequest
    {
        private readonly RequestDelegate request;
        public PreOptionRequest(RequestDelegate request)
        {
            this.request = request;
        }
        public async Task Invoke(HttpContext context)
        {
            if(context.Request.Method.ToUpper() == "OPTIONS")
            {
                context.Response.StatusCode = 200;
                return;
            }
            await request.Invoke(context);
        }
    }
    /// <summary>
    /// 处理Option请求类型的中间件
    /// </summary>
    public static class PreOptionRequestExtensions
    {
        public static IApplicationBuilder UsePreOptionRequest(this IApplicationBuilder app)
        {
            return app.UseMiddleware<PreOptionRequest>();
        }
    }
}
